package com.zidongxiangxi.rabbit.core.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 抽象工作线程类
 *
 * @author chenxudong
 * @date 2020/11/07
 */
public abstract class AbstractWorkThread implements Runnable {
	Logger logger = LoggerFactory.getLogger(AbstractWorkThread.class);

	protected volatile boolean stop = false;

	@Override
	public void run() {
		logger.info("start {}......", this.getClass().toString());
		while (!isStop()) {
			try {
				work();
			} catch (Throwable e) {
				logger.error("fail to execute work", e);
			}
		}
		logger.info("stop {}......", this.getClass().toString());
	}

	/**
	 * 执行线程工作
	 */
	public abstract void work();

	/**
	 * 线程是否需要停止
	 *
	 * @return 是/否
	 */
	public boolean isStop() {
		return stop;
	}

	/**
	 * 停止线程
	 */
	public void stop() {
		stop = true;
	}
}

